package com.twang.stock.strategy;

import com.alibaba.fastjson.JSONObject;
import com.jayway.jsonpath.JsonPath;
import com.twang.stock.common.CaiLianSeCommonUtils;
import com.twang.stock.config.StockMsgSender;
import com.twang.stock.dto.EmailMessage;
import com.twang.stock.entity.User;
import com.twang.stock.mapper.UserMapper;
import com.twang.stock.redis.RedisUtil;
import com.twang.stock.service.jinyan.CommonService;
import com.twang.stock.utils.RedisKey;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.common.base.select.SelectByPrimaryKeyMapper;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;


/**
 * 预警重要信息
 */
@Service
public class WarnVipNewsService {

    public static String _RECOMMEND="recommend";
    public static String _CONTENT="content";
    public static String _SHAREURL="shareurl";

    public static String _ID="id";

    @Autowired
    private CommonService commonService;
    @Resource
    private UserMapper userMapper;

    @Autowired
    private RedisUtil redisUtil;

    @Autowired
    private StockMsgSender stockMsgSender;


    public void process() throws Exception {
        String result = CaiLianSeCommonUtils.查询财联社头条预警();
        List<Map> list = JsonPath.read(result, "$.data.roll_data");
        for (int i = 0; i < list.size(); i++) {
            Map item = list.get(i);
            String recommend = item.get(_RECOMMEND).toString();//是否重要
            if ("1".equals(recommend)) {
                Map resultMsg = resultMap(item);
                String redisKey = String.format(RedisKey.LOCK_NEW_MINUTES_ID, resultMsg.get(_ID));
                if (redisUtil.hasKey(redisKey)) {
                    continue;
                }
                //邮件
                //触发预警
                String message = resultMsg.get(_CONTENT).toString();
                String regex = "\\【(.*?)\\】"; // 正则表达式，用来找到【】中的内容
                Pattern pattern = Pattern.compile(regex);
                Matcher matcher = pattern.matcher(message);
                String title = "";
                if (matcher.find()) {
                    title = matcher.group(1); // 输出【】中的内容
                } else {
                    System.out.println("没有找到匹配的内容");
                    if(message.length()>12){
                        title=message.substring(1,12);
                    }else {
                        title=message;
                    }
                }
                String messages = new StringBuffer().append(message).toString();
                EmailMessage emailMessage = new EmailMessage();
                emailMessage.setCode(title);
                emailMessage.setName(title);
                emailMessage.setMessage(messages);
                emailMessage.setType("6");
                redisUtil.set(redisKey, resultMsg.get(_ID), 60 * 60 * 23);
                String[] ids = commonService.getValue("WARN_NEWS_ID").split(",");
                for (String id : ids) {
                    User user = userMapper.selectByPrimaryKey(Long.valueOf(id));
                    emailMessage.setEmail(user.getEmail());
                    stockMsgSender.sendEmailMessage(JSONObject.toJSONString(emailMessage));
                }

            }
        }
    }

    private static Map resultMap(Map item) {
        String content = item.get(_CONTENT).toString();
        String id = item.get(_ID).toString();
        Object shareurl = item.get(_SHAREURL);
        Map map = new HashMap<>();
        if (null != shareurl) {
            map.put(_SHAREURL, shareurl.toString());
        }
        map.put(_CONTENT, content);
        map.put(_ID, id);
        return map;
    }
}
